package com.ruoyi.waterManagement.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.business.waterManagement.WrStatvolD;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.business.BusinessUtils;
import com.ruoyi.waterManagement.domain.vo.*;
import com.ruoyi.waterManagement.service.IWaterRegimeMonitoringService;
import com.ruoyi.waterManagement.service.IWrStatvolDService;
import com.ruoyi.waterManagement.service.IWrStatvolHService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 测站日水量Controller
 * 
 * @author hwd
 * @date 2023-02-20
 */
@RestController
@RequestMapping("/WrStatvolD")
public class WrStatvolDController extends BaseController
{
    @Autowired
    private IWrStatvolDService wrStatvolDService;

    @Autowired
    private IWrStatvolHService wrStatvolHService;

    @Autowired
    private IWaterRegimeMonitoringService waterRegimeMonitoringService;
    /**
     * 查询测站日水量列表
     */
    @RequiresPermissions("waterManagement:WrStatvolD:list")
    @GetMapping("/list")
    public TableDataInfo list(WrStatvolD wrStatvolD)
    {
        if (wrStatvolD.getStatDay()==null){
        wrStatvolD.setStatDay(new Date());
        }
        startPage();
        List<WrStatvolD> list = wrStatvolDService.selectWrStatvolDList(wrStatvolD);
        if (list.size()>0){
            String statNm= waterRegimeMonitoringService.queryStatNm(wrStatvolD.getStatCd());
            list.forEach(WrStatvolD->WrStatvolD.setStatNm(statNm));
        }
        return getDataTable(list);
    }

    /**
     * 导出测站日水量列表
     */
    @RequiresPermissions("waterManagement:WrStatvolD:export")
    @Log(title = "测站日水量", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WrStatvolD wrStatvolD)
    {
        List<WrStatvolD> list = wrStatvolDService.selectWrStatvolDList(wrStatvolD);
        ExcelUtil<WrStatvolD> util = new ExcelUtil<WrStatvolD>(WrStatvolD.class);
        util.exportExcel(response, list, "测站日水量数据");
    }

    /**
     * 导出水管机构日报
     */
    @RequiresPermissions("waterManagement:WrStatvolD:export")
    @Log(title = "测站日水量", businessType = BusinessType.EXPORT)
    @PostMapping("/exportDailyWaterReport")
    public AjaxResult export(HttpServletResponse response, WaterReportVO waterReportVO) throws IOException {
        int yy=1970;
        int mm=1;
        int dd =1;
        if (waterReportVO.getDd()==null){
           waterReportVO.setDd(new Date());
        }
        Calendar calendar= Calendar.getInstance();
        calendar.setTime(waterReportVO.getDd());
        yy=calendar.get(Calendar.YEAR);
        mm=calendar.get(Calendar.MONTH)+1;
        dd=calendar.get(Calendar.DATE);
        if (waterReportVO.getOrgNm()==null){
            return AjaxResult.error("请选择水管机构");
        }
        String excelTitle = waterReportVO.getOrgNm() +yy+"年"+mm+"月"+dd+"日" + "水量日报";
        wrStatvolDService.exportExcel(waterReportVO, response, excelTitle);
        return AjaxResult.success("1");
    }

    /**
     * 获取测站日水量详细信息
     */
    @RequiresPermissions("waterManagement:WrStatvolD:query")
    @GetMapping(value = "/{statCd}")
    public AjaxResult getInfo(@PathVariable("statCd") String statCd)
    {
        return success(wrStatvolDService.selectWrStatvolDByStatCd(statCd));
    }

    /**
     * 新增测站日水量
     */
    @RequiresPermissions("waterManagement:WrStatvolD:add")
    @Log(title = "测站日水量", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WrStatvolD wrStatvolD)
    {
        return toAjax(wrStatvolDService.insertWrStatvolD(wrStatvolD));
    }

    /**
     * 修改测站日水量
     */
    @RequiresPermissions("waterManagement:WrStatvolD:edit")
    @Log(title = "测站日水量", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WrStatvolD wrStatvolD)
    {
        return toAjax(wrStatvolDService.updateWrStatvolD(wrStatvolD));
    }

    /**
     * 删除测站日水量
     */
    @RequiresPermissions("waterManagement:WrStatvolD:remove")
    @Log(title = "测站日水量", businessType = BusinessType.DELETE)
	@DeleteMapping("/{statCds}")
    public AjaxResult remove(@PathVariable String[] statCds)
    {
        return toAjax(wrStatvolDService.deleteWrStatvolDByStatCds(statCds));
    }

    /**
     * 统计测站水量月逐日
     * @param yzrWrStatvolDVO
     * @return
     */

    @GetMapping("/listWrStatvolDVO")
    public AjaxResult list(YzrWrStatvolDVO yzrWrStatvolDVO)
    {

        /**入参行政区别、渠道等级、日期，测站
         */
        //判断关键参数日期是否存在
        Date wrdate=yzrWrStatvolDVO.getStatDay();
        if(wrdate==null||wrdate.after(DateUtils.getNowDate())){
            //晚于当前日期或者没有日期，则改为当前日期
            yzrWrStatvolDVO.setStatDay(DateUtils.getNowDate());
        }
        if (StringUtils.isEmpty(yzrWrStatvolDVO.getOrgCd())) {
            yzrWrStatvolDVO.setOrgCd(null);
        }
        //找到下属的所有水管机构的统计
        HashMap<String, Object> addvAndOrgCodeInfo = BusinessUtils.getAddvAndOrgCodeInfo(yzrWrStatvolDVO.getOrgCd());
        int grade = (int) addvAndOrgCodeInfo.get("grade");
        String resCode = (String) addvAndOrgCodeInfo.get("resCode");
        yzrWrStatvolDVO.setOrgCd(resCode+"%");
        List<YzrWrStatvolDVO> yzrList = wrStatvolDService.selectYzrWrStatvolDVOList(yzrWrStatvolDVO);
        if (grade == 1){
            //查找下属水管机构
            resCode =  resCode + "%0000";
        }else if (grade == 2) {
            //查找下属水管机构
            resCode =resCode + "%00";
        }else if(grade ==3) {
            resCode =resCode+"%";
        }else {
            resCode = resCode;
        }
        //查找下属机构
        RzsWrStatvolHwrVO rzsWrStatvolHwrVO=new RzsWrStatvolHwrVO();
        rzsWrStatvolHwrVO.setOrgCd(resCode);
        List<RzsWrStatvolHwrVO> orglist = wrStatvolHService.listOrgCd(rzsWrStatvolHwrVO);
        List<YzrWrStatvolDVO> orgnewList =new ArrayList<>();
        //遍历查询下属水管机构统计
        for (RzsWrStatvolHwrVO r:orglist
        ) {
            HashMap<String, Object> stringObjectHashMap = BusinessUtils.getAddvAndOrgCodeInfo(r.getOrgCd());
            String ro = (String) stringObjectHashMap.get("resCode");
            List<YzrWrStatvolDVO> yzrWrStatvolDVOList = yzrList.stream().filter(s -> s.getOrgCd().startsWith(ro)).collect(Collectors.toList());
            YzrWrStatvolDVO wrVoSum =new YzrWrStatvolDVO();
            wrVoSum.setOrgNm(r.getOrgNm());
            BigDecimal s1 =new BigDecimal("0");
            BigDecimal s2 =new BigDecimal("0");
            BigDecimal s3 =new BigDecimal("0");
            BigDecimal s4 =new BigDecimal("0");
            BigDecimal s5 =new BigDecimal("0");
            BigDecimal s6 =new BigDecimal("0");
            BigDecimal s7 =new BigDecimal("0");
            BigDecimal s8 =new BigDecimal("0");
            BigDecimal s9 =new BigDecimal("0");
            BigDecimal s10 =new BigDecimal("0");
            BigDecimal s11 =new BigDecimal("0");
            BigDecimal s12 =new BigDecimal("0");
            BigDecimal s13 =new BigDecimal("0");
            BigDecimal s14 =new BigDecimal("0");
            BigDecimal s15 =new BigDecimal("0");
            BigDecimal s16 =new BigDecimal("0");
            BigDecimal s17 =new BigDecimal("0");
            BigDecimal s18 =new BigDecimal("0");
            BigDecimal s19 =new BigDecimal("0");
            BigDecimal s20 =new BigDecimal("0");
            BigDecimal s21 =new BigDecimal("0");
            BigDecimal s22 =new BigDecimal("0");
            BigDecimal s23 =new BigDecimal("0");
            BigDecimal s24=new BigDecimal("0");
            BigDecimal s25 =new BigDecimal("0");
            BigDecimal s26 =new BigDecimal("0");
            BigDecimal s27 =new BigDecimal("0");
            BigDecimal s28 =new BigDecimal("0");
            BigDecimal s29 =new BigDecimal("0");
            BigDecimal s30 =new BigDecimal("0");
            BigDecimal s31 =new BigDecimal("0");
            BigDecimal sc =new BigDecimal("0");
            if (yzrWrStatvolDVOList.size()>0) {
                for (YzrWrStatvolDVO w : yzrWrStatvolDVOList
                ) {
                    s1 = w.getS1() == null ? s1.add(new BigDecimal("0")) : s1.add(new BigDecimal(w.getS1())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s2 = w.getS2() == null ? s2.add(new BigDecimal("0")) : s2.add(new BigDecimal(w.getS2())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s3 = w.getS3() == null ? s3.add(new BigDecimal("0")) : s3.add(new BigDecimal(w.getS3())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s4 = w.getS4() == null ? s4.add(new BigDecimal("0")) : s4.add(new BigDecimal(w.getS4())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s5 = w.getS5() == null ? s5.add(new BigDecimal("0")) : s5.add(new BigDecimal(w.getS5())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s6 = w.getS6() == null ? s6.add(new BigDecimal("0")) : s6.add(new BigDecimal(w.getS6())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s7 = w.getS7() == null ? s7.add(new BigDecimal("0")) : s7.add(new BigDecimal(w.getS7())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s8 = w.getS8() == null ? s8.add(new BigDecimal("0")) : s8.add(new BigDecimal(w.getS8())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s9 = w.getS9() == null ? s9.add(new BigDecimal("0")) : s9.add(new BigDecimal(w.getS9())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s10 = w.getS10() == null ? s10.add(new BigDecimal("0")) : s10.add(new BigDecimal(w.getS10())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s11 = w.getS11() == null ? s11.add(new BigDecimal("0")) : s11.add(new BigDecimal(w.getS11())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s12 = w.getS12() == null ? s12.add(new BigDecimal("0")) : s12.add(new BigDecimal(w.getS12())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s13 = w.getS13() == null ? s13.add(new BigDecimal("0")) : s13.add(new BigDecimal(w.getS13())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s14 = w.getS14() == null ? s14.add(new BigDecimal("0")) : s14.add(new BigDecimal(w.getS14())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s15 = w.getS15() == null ? s15.add(new BigDecimal("0")) : s15.add(new BigDecimal(w.getS15())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s16 = w.getS16() == null ? s16.add(new BigDecimal("0")) : s16.add(new BigDecimal(w.getS16())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s17 = w.getS17() == null ? s17.add(new BigDecimal("0")) : s17.add(new BigDecimal(w.getS17())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s18 = w.getS18() == null ? s18.add(new BigDecimal("0")) : s18.add(new BigDecimal(w.getS18())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s19 = w.getS19() == null ? s19.add(new BigDecimal("0")) : s19.add(new BigDecimal(w.getS19())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s20 = w.getS20() == null ? s20.add(new BigDecimal("0")) : s20.add(new BigDecimal(w.getS20())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s21 = w.getS21() == null ? s21.add(new BigDecimal("0")) : s21.add(new BigDecimal(w.getS21())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s22 = w.getS22() == null ? s22.add(new BigDecimal("0")) : s22.add(new BigDecimal(w.getS22())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s23 = w.getS23() == null ? s23.add(new BigDecimal("0")) : s23.add(new BigDecimal(w.getS23())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s24 = w.getS24() == null ? s24.add(new BigDecimal("0")) : s24.add(new BigDecimal(w.getS24())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s25 = w.getS25() == null ? s25.add(new BigDecimal("0")) : s25.add(new BigDecimal(w.getS25())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s26 = w.getS26() == null ? s26.add(new BigDecimal("0")) : s26.add(new BigDecimal(w.getS26())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s27 = w.getS27() == null ? s27.add(new BigDecimal("0")) : s27.add(new BigDecimal(w.getS27())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s28 = w.getS28() == null ? s28.add(new BigDecimal("0")) : s28.add(new BigDecimal(w.getS28())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s29 = w.getS29() == null ? s29.add(new BigDecimal("0")) : s29.add(new BigDecimal(w.getS29())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s30 = w.getS30() == null ? s30.add(new BigDecimal("0")) : s30.add(new BigDecimal(w.getS30())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    s31 = w.getS31() == null ? s31.add(new BigDecimal("0")) : s31.add(new BigDecimal(w.getS31())).setScale(4, BigDecimal.ROUND_HALF_UP);
                    sc = w.getSc() == null ? sc.add(new BigDecimal("0")) : sc.add(new BigDecimal(w.getSc()));
                }
            }
            wrVoSum.setOrgCd("合计");
            wrVoSum.setS1(s1.toString());
            wrVoSum.setS2(s2.toString());
            wrVoSum.setS3(s3.toString());
            wrVoSum.setS4(s4.toString());
            wrVoSum.setS5(s5.toString());
            wrVoSum.setS6(s6.toString());
            wrVoSum.setS7(s7.toString());
            wrVoSum.setS8(s8.toString());
            wrVoSum.setS9(s9.toString());
            wrVoSum.setS10(s10.toString());
            wrVoSum.setS11(s11.toString());
            wrVoSum.setS12(s12.toString());
            wrVoSum.setS13(s13.toString());
            wrVoSum.setS14(s14.toString());
            wrVoSum.setS15(s15.toString());
            wrVoSum.setS16(s16.toString());
            wrVoSum.setS17(s17.toString());
            wrVoSum.setS18(s18.toString());
            wrVoSum.setS19(s19.toString());
            wrVoSum.setS20(s20.toString());
            wrVoSum.setS21(s21.toString());
            wrVoSum.setS22(s22.toString());
            wrVoSum.setS23(s23.toString());
            wrVoSum.setS24(s24.toString());
            wrVoSum.setS25(s25.toString());
            wrVoSum.setS26(s26.toString());
            wrVoSum.setS27(s27.toString());
            wrVoSum.setS28(s28.toString());
            wrVoSum.setS29(s29.toString());
            wrVoSum.setS30(s30.toString());
            wrVoSum.setS31(s31.toString());
            wrVoSum.setSc(sc.toString());
            orgnewList.add(0,wrVoSum);
        }
            orgnewList.removeAll(Collections.singleton(null));
            //将数据结果放入map中区分时
            Collections.reverse(orgnewList);
            //将数据结果放入map中区分时
        return AjaxResult.success(orgnewList);
    }


    /**
     * 查询取用水测站年逐日水量信息列表
     */
    @GetMapping("/listNzrWrStatvolDVO")
    public AjaxResult listNzrWrStatvolDVO(NzrWrStatvolDVO nzrWrStatvolDVO)
    {
        /**入参水管机构 日期
         */
        //判断关键参数日期是否存在
        String wrdate=nzrWrStatvolDVO.getYy();
        if(StringUtils.isNotEmpty(wrdate)){
            //晚于当前日期或者没有日期，则改为当前日期
        }else{
            wrdate=DateUtils.parseDateToStr(DateUtils.YYYY,new Date());
        }
        nzrWrStatvolDVO.setYy(wrdate);
        //TODO 写死一个变量
        HashMap map = wrStatvolDService.selectNzrWrStatvolDVO(nzrWrStatvolDVO);
        //将数据结果放入map中区分时
        return AjaxResult.success(map);
    }

    /**
     * 查询取用水测站年逐日水位信息列表
     */

    @GetMapping("/listNzrLevelVO")
    public AjaxResult listNzrLevelVO(NzrWrStatvolDVO nzrWrStatvolDVO)
    {
        /**入参水管机构 日期
         */
        //判断关键参数日期是否存在
        String wrdate=nzrWrStatvolDVO.getYy();
        if(StringUtils.isNotEmpty(wrdate)){
            //晚于当前日期或者没有日期，则改为当前日期
        }else{
            wrdate=DateUtils.parseDateToStr(DateUtils.YYYY,new Date());
        }
        nzrWrStatvolDVO.setYy(wrdate);
        HashMap map = wrStatvolDService.selectNzrWaterLevelVO(nzrWrStatvolDVO);
        //将数据结果放入map中区分时
        return AjaxResult.success(map);
    }

    /**
     * 查询取用水测站年逐日流量信息列表
     */
    @GetMapping("/listNzrFlowVO")
    public AjaxResult listNzrFlowVO(NzrWrStatvolDVO nzrWrStatvolDVO)
    {
        /**入参水管机构 日期
         */
        //判断关键参数日期是否存在
        String wrdate=nzrWrStatvolDVO.getYy();
        if(StringUtils.isNotEmpty(wrdate)){
            //晚于当前日期或者没有日期，则改为当前日期
        }else{
            wrdate=DateUtils.parseDateToStr(DateUtils.YYYY,new Date());
        }
        nzrWrStatvolDVO.setYy(wrdate);
        HashMap map = wrStatvolDService.selectNzrFlowVO(nzrWrStatvolDVO);
        //将数据结果放入map中区分时
        return AjaxResult.success(map);
    }


    /**
     * 查询水管机构水量日报
     */
    @GetMapping("/listDailyWaterReport")
    public AjaxResult listDailyWaterReport(WaterReportVO dayWaterReportVO){
        return AjaxResult.success(wrStatvolDService.selectDayWaterReport(dayWaterReportVO));
    }
}
